package com.dlc.shop.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.model.Product;
import com.dlc.shop.bean.model.PurchaseOrder;
import com.dlc.shop.bean.model.PurchaseProd;
import com.dlc.shop.bean.model.Sku;
import com.dlc.shop.dao.PurchaseOrderMapper;
import com.dlc.shop.dao.PurchaseProdMapper;
import com.dlc.shop.service.ProductService;
import com.dlc.shop.service.PurchaseProdService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 *
 * @author YXF
 * @date 2021-09-08 10:42:00
 */
@Service
@AllArgsConstructor
public class PurchaseProdServiceImpl extends ServiceImpl<PurchaseProdMapper, PurchaseProd> implements PurchaseProdService {

    private final PurchaseProdMapper purchaseProdMapper;
    private final PurchaseOrderMapper purchaseOrderMapper;
    private final ProductService productService;

    @Override
    public void loadProdAndSkuInfo(List<PurchaseProd> purchaseProds) {
        Set<Long> prodIds = new HashSet<>();
        List<Long> skuIds = new ArrayList<>();
        for (PurchaseProd purchaseProd : purchaseProds) {
            prodIds.add(purchaseProd.getProdId());
            skuIds.add(purchaseProd.getSkuId());
        }
        // 普通商品信息
        Map<Long, Product> prodMap;
        if (CollUtil.isNotEmpty(prodIds) && CollUtil.isNotEmpty(skuIds)) {
            List<Product> products = productService.prodAndSkuInfo(new ArrayList<>(prodIds), skuIds);
            prodMap = products.stream().collect(Collectors.toMap(Product::getProdId, p -> p));
        } else {
            prodMap = new HashMap<>(0);
        }
        for (PurchaseProd purchaseProd : purchaseProds) {
            if (!prodMap.containsKey(purchaseProd.getProdId())) {
                continue;
            }
            Product product = prodMap.get(purchaseProd.getProdId());
            Sku sku = null;
            for (Sku skuDb : product.getSkuList()) {
                if (Objects.equals(skuDb.getSkuId(), purchaseProd.getSkuId())) {
                    sku = skuDb;
                    break;
                }
            }
            if (Objects.isNull(sku)) {
                return;
            }
            purchaseProd.setPic(product.getPic());
            purchaseProd.setProdName(product.getProdName());
            purchaseProd.setSkuName(sku.getSkuName());
            purchaseProd.setPartyCode(sku.getPartyCode());
            purchaseProd.setIsDelete(sku.getIsDelete());
        }
    }

    @Override
    public void deleteByProdId(Long prodId, List<Long> skuIds) {
        if (Objects.isNull(prodId) && CollUtil.isEmpty(skuIds)) {
            return;
        }
        List<String> purchaseOrderNumbers = purchaseProdMapper.listPurchaseNumberByProdId(prodId, skuIds);
        if(CollUtil.isEmpty(purchaseOrderNumbers)) {
            return;
        }
        List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.listInfoByPurchaseOrderNumbers(purchaseOrderNumbers);
        List<PurchaseProd> purchaseProds = new ArrayList<>();
        List<PurchaseOrder> updateList = new ArrayList<>();
        if (Objects.isNull(skuIds)) {
            skuIds = new ArrayList<>();
        }
        for (PurchaseOrder purchaseOrder : purchaseOrders) {
            Boolean allDelete = true;
            for (PurchaseProd purchaseProd : purchaseOrder.getPurchaseProds()) {
                boolean nullify = Objects.equals(purchaseProd.getProdId(), prodId) || skuIds.contains(purchaseProd.getSkuId());
                if (nullify) {
                    purchaseProd.setStatus(0);
                    purchaseProds.add(purchaseProd);
                }
                if (!Objects.equals(purchaseProd.getStatus(), 0)) {
                    allDelete = false;
                }
            }
            if(allDelete) {
                purchaseOrder.setStatus(0);
                updateList.add(purchaseOrder);
            }
        }
        if (CollUtil.isNotEmpty(updateList)) {
            purchaseOrderMapper.updateBatchStatus(updateList);
        }
        if (CollUtil.isNotEmpty(purchaseProds)) {
            updateBatchById(purchaseProds);
        }
    }
}
